// ==PREPROCESSOR==
// @name "Drag Drop Basic"
// @author "T.P Wang"
// Add feature below to enable drag and drop support
// @feature "dragdrop"
// @import "%fb2k_component_path%docs\helpers.txt"
// ==/PREPROCESSOR==

// RGB function is defined in docs\helpers.txt
var color_background = RGB(255, 255, 255);
var color_background2 = RGB(193, 219, 252);
var dragging = false;

function on_paint(gr) {
	var color = dragging ? color_background2 : color_background;
	gr.FillSolidRect(0, 0, window.Width, window.Height, color);
}

function on_drag_enter() {
	dragging = true;
	window.Repaint();
}

function on_drag_leave() {
	dragging = false;
	window.Repaint();
}

// Here we create a new playlist for hosting the dropped objects.
function on_drag_drop(action) {
	var playlist_name = 'Dropped Items';
	var idx = -1;
	
	// Find the playlist first.
	for (var i = 0; i < plman.PlaylistCount; ++i) {
		if (plman.GetPlaylistName(i) == playlist_name) {
			idx = i;
			break;
		}
	}
	
	// If not found, then create one.
	if (idx == -1)
		idx = plman.CreatePlaylist(plman.PlaylistCount, playlist_name);
	
	// This is required because when a drop event is fired, you won't get on_drag_leave() called.
	dragging = false;
	window.Repaint();
	
	// We are going to process the dropped items to a playlist.
	action.ToPlaylist();
	action.Playlist = idx;
	action.ToSelect = false;
	
	// Switch to "Dropped Items" playlist
	plman.ActivePlaylist = idx;
}
